package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.dao.OrderDetailRepository;
import com.qf.dao.OrderRepository;
import com.qf.dao.QfOrderDao;
import com.qf.pojo.RedisKey;
import com.qf.pojo.reqVo.OrderReq;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.QfCourse;
import com.qf.pojo.vo.QfOrder;
import com.qf.pojo.vo.QfOrderDetail;
import com.qf.service.OrderService;
import com.qf.utils.GenerateOrderNum;
import com.qf.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserUtils userUtils;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    OrderDetailRepository orderDetailRepository;

    @Autowired
    QfOrderDao qfOrderDao;

    @Override
    @Transactional
    public BaseResp addOrder(OrderReq orderReq, HttpServletRequest request) {
        Integer userId = userUtils.getUserId(request);
       //用户的判断
        if (userId==0){
            return new BaseResp(0,"用户未登录！",null,null);
        }
        //1.参数判断
        if (orderReq.getIds()==null||orderReq.getIds().size()==0){
            return new BaseResp(0,"必须有商品！",null,null);
        }
        //比对金额,我们从redis中获取到该课程的详情信息，进行金额的相加与前台的金额进行比较
        Double totalMoney=0.0;
        //循环前台传入如的课程的集合id
        for (Integer id:orderReq.getIds()
             ) {
            //通过课程id从购物车中获取到该课程的详情信息
            Object o = redisTemplate.opsForHash().get(RedisKey.SHOPCARD_ + userId.toString(), id.toString());
            //进行转换
            String s = JSONObject.toJSONString(o);
            QfCourse qfCourse = JSONObject.parseObject(s, QfCourse.class);
            totalMoney=totalMoney+qfCourse.getPay();
        }
        //进行价格的比较，使用后台计算的金额与前台传入的金额进行比较
        if (totalMoney.compareTo(orderReq.getMoney())!=0){
            return new BaseResp(0,"金额不匹配！",null,null);
        }
        //进行总订单的生成
        QfOrder qfOrder = new QfOrder();
        //设置订单的id
        String orderId = GenerateOrderNum.generate("qf");
        qfOrder.setOrderid(orderId);
        //设置下单的用户
        qfOrder.setUserid(userId);
        //设置订单的金额
        qfOrder.setOrderPay(totalMoney);
        //设置订单的状态
        qfOrder.setOrderStatus(1);
        //设置订单的时间
        qfOrder.setCreatetime(new Date());
        //添加到数据库
        orderRepository.save(qfOrder);
        //添加子订单
        for (Integer id:orderReq.getIds()
             ) {
            //声明一个对象
            QfOrderDetail qfOrderDetail = new QfOrderDetail();
            //放置子订单的Id
            qfOrderDetail.setOrderDetailId(GenerateOrderNum.generate("detail"));
            //设置总订单的id
            qfOrderDetail.setOrderid(orderId);
            //设置课程id
            qfOrderDetail.setCourseid(id);
            //设置时间
            qfOrderDetail.setCreatetime(new Date());
            //入库
            orderDetailRepository.save(qfOrderDetail);
            //生成订单后，将购物车的该课程进行移除
            redisTemplate.opsForHash().delete(RedisKey.SHOPCARD_+userId.toString(),id.toString());
        }


        return new BaseResp(1,"生成成功！",null,null);
    }

    @Override
    public BaseResp findOrderByUserId(HttpServletRequest request,OrderReq orderReq) {
        Integer userId = userUtils.getUserId(request);
        //用户的判断
        if (userId==0){
            return new BaseResp(0,"用户未登录！",null,null);
        }
        List<QfOrder> orderListByUserIdAndStatus=null;
        if (orderReq.getOrderstatus()==1){
            orderListByUserIdAndStatus   = qfOrderDao.findOrderListByUserIdAndStatus(userId, 1);

        }else{
            orderListByUserIdAndStatus  = qfOrderDao.findOrderListByUserIdAndStatus(userId, null);

        }
        return new BaseResp(1,"查询成功！",orderListByUserIdAndStatus,null);
    }

    @Override
    public QfOrder findById(String id) {
        return orderRepository.findById(id).get();
    }

    @Override
    public QfOrder updateById(QfOrder qfOrder) {
        return orderRepository.saveAndFlush(qfOrder);
    }
}
